/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlutils;

import DTO.song.Artist;
import DTO.song.Artists;
import DTO.song.Genre;
import DTO.song.Likers;
import DTO.song.Song;
import DTO.song.Songs;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.math.BigInteger;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import utils.VietnameseConvert;

/**
 *
 * @author NoiR
 */
public class SearchUtils {

    private final String songXML = "/xml/Songs.xml";
    private Boolean found = false;
    private VietnameseConvert convertor = null;

    public Songs seachSong(String realPath, String query) {
        Songs rs = null;
        Song song = null;
        Artists artists = null;
        Artist artist = null;
        Genre genre = null;
        Likers likers = null;
        String username = "";

        InputStream is = null;
        XMLStreamReader xsr = null;
        String filePath = realPath + songXML;
        convertor = new VietnameseConvert();
        try {
            XMLInputFactory xif = XMLInputFactory.newInstance();
            File file = new File(filePath);
            is = new FileInputStream(file);
            xsr = xif.createXMLStreamReader(is);

            rs = new Songs();
            while (xsr.hasNext()) {
                int event = xsr.next();
                if (event == XMLStreamConstants.START_ELEMENT) {
                    String eleName = xsr.getLocalName();
                    if (eleName.equals("Song")) {
                        if (found) {
                            rs.getSong().add(song);
                            found = false;
                        } else {
                            song = new Song();
                        }

                        xsr.nextTag(); // SongID
                        xsr.next(); // value
                        song.setSongID(BigInteger.valueOf(Integer.parseInt(xsr.getText().trim())));
                        xsr.nextTag(); // end SongID
                        xsr.nextTag(); // isMV
                        xsr.next();
                        song.setIsMV(Boolean.parseBoolean(xsr.getText().trim()));
                        xsr.nextTag(); // end isMV
                        xsr.nextTag(); // SongTitle
                        xsr.next(); // value
                        String title = xsr.getText().trim();
                        title = convertor.toStringNoAccent(title).toLowerCase();
                        // check query and song title
                        if (title.contains(query)) {
                            found = true;
                            song.setSongTitle(xsr.getText().trim());
                            xsr.nextTag(); // end SongTitle
                            xsr.nextTag(); // SongViews
                            xsr.next();
                            song.setSongViews(BigInteger.valueOf(Integer.parseInt(xsr.getText().trim())));
                            xsr.nextTag(); // end SongViews
                            xsr.nextTag();
                            xsr.nextTag();
                            xsr.nextTag(); // SongSRC
                            xsr.next(); // value
                            song.setSongSRC(xsr.getText().trim());
                            xsr.nextTag(); // end SongSRC
                            xsr.nextTag(); // LikeNumber
                            xsr.next();
                            song.setLikeNumber(BigInteger.valueOf(Integer.parseInt(xsr.getText().trim())));
                            xsr.nextTag(); // end LikeNumber
                            xsr.nextTag(); // Move to next tag
                        }
                    } else if (eleName.equals("Lyrics")) {
                        if (found) {
                            xsr.next();
                            song.setLyrics(xsr.getText().trim());
                        }
                    } else if (eleName.equals("SongDescription")) {
                        if (found) {
                            xsr.next();
                            song.setSongDescription(xsr.getText().trim());
                        }
                    } else if (eleName.equals("Artist")) {
                        if (found) {
                            if (artists == null) {
                                artists = new Artists();
                            }
                            artist = new Artist();

                            xsr.nextTag(); // ArtistID
                            xsr.next();
                            artist.setArtistID(BigInteger.valueOf(Integer.parseInt(xsr.getText().trim())));
                            xsr.nextTag(); // end ArtistID
                            xsr.nextTag(); // ArtistName
                            xsr.next();
                            String name = xsr.getText().trim();
                            artist.setArtistName(name);
                            xsr.nextTag(); // end ArtistName
                            xsr.nextTag(); // to nextTag()

                            artists.getArtist().add(artist);
                            xsr.nextTag();
                        }
                    } else if (eleName.equals("Genre")) {
                        if (found) {
                            song.setArtists(artists);
                            genre = new Genre();

                            xsr.nextTag(); // GenreID
                            xsr.next();
                            genre.setGenreID(BigInteger.valueOf(Integer.parseInt(xsr.getText().trim())));
                            xsr.nextTag(); // end GenreID
                            xsr.nextTag(); // GenreName
                            xsr.next();
                            genre.setGenreName(xsr.getText().trim());
                            xsr.nextTag(); // end GenreName
                            xsr.nextTag(); // move to nextTag();

                        }
                    } else if (eleName.equals("Likers")) {
                        if (found) {
                            song.setGenre(genre);
                            likers = new Likers();
                            xsr.nextTag(); // to nextTag()
                        }
                    } else if (eleName.equals("Username")) {
                        if (found) {
                            username = "";
                            // song.setLikers(likers);
                            xsr.next(); // to nextTag()
                            username = xsr.getText().trim();
                            likers.getUsername().add(username);
                            xsr.nextTag();
                        }
                    } else if (eleName.equals("SongDateCreated")) {
                        if (found) {
                            song.setLikers(likers);
                            xsr.next();
                            XMLGregorianCalendar xgcal =
                                    DatatypeFactory.newInstance().newXMLGregorianCalendar(xsr.getText().trim());
                            song.setSongDateCreated(xgcal);
                            xsr.nextTag();
                        }
                    }else if(eleName.equals("AlbumID")){
                        if(found){
                            xsr.next(); // to value
                            song.setAlbumID(BigInteger.valueOf(Integer.parseInt(xsr.getText().trim())));
                            xsr.nextTag(); // end albumid
                            xsr.nextTag(); // start albumtitle
                            xsr.next(); // to value
                            song.setAlbumTitle(xsr.getText().trim());
                            xsr.nextTag(); // end albumtitle
                            xsr.nextTag(); // trackNumber
                            xsr.next(); // to value
                            song.setAlbumID(BigInteger.valueOf(Integer.parseInt(xsr.getText().trim())));
                            xsr.nextTag(); // end tracknumber
                        }
                    }
                } // end StartElement
            } // end
            return rs;
        } catch (DatatypeConfigurationException ex) {
            System.out.println("Parse date error: "+ex.getMessage());
            return rs;
        } catch (XMLStreamException ex) {
            System.out.println("XML Stream reader error: "+ex.getMessage());
            return rs;
        } catch (FileNotFoundException ex) {
            System.out.println("File Not Found error: "+ex.getMessage());
            return rs;
        }
    }

    // ThuanNH
    // Order song by Views
    public Songs orderSong(Songs input) {
        Song tmp = new Song();
        for (int i = 0; i < getLength(input); i++) {
            for (int j = 1; j <= getLength(input) - 1; j++) {

                Song s1 = input.getSong().get(j);
                Song s2 = input.getSong().get(j - 1);

                // if the second song has views number bigger than the first song,
                // swap them.
                if (s2.getSongViews().intValue() < s1.getSongViews().intValue()) {
                    // swap
                    tmp = s1;
                    input.getSong().set(j, s2);
                    input.getSong().set(j - 1, tmp);
                } // end
            } // end for j
        } // end for i
        return input;
    }

    // ThuanNH
    // get length of Songs
    public int getLength(Songs input) {
        return input.getSong().size();
    }
}
